﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GGEditor.API.Documents;
using GGEditor.API.Editors;

namespace GGEditor.API.Samples.ArcadeGame
{
    public partial class EditorView : UserControl, IEditorView
    {
        private const int defaultRadius = 20;

        IList<Circle> circles;

        public EditorView()
        {
            InitializeComponent();

            circles = new List<Circle>();

            SetStyle(ControlStyles.UserPaint |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.ResizeRedraw |
                ControlStyles.DoubleBuffer, true);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            // Even with double-buffering, do nothing here.
            // Avoid flicker by doing everything in OnPaint.
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            using (Brush bg = new SolidBrush(BackColor))
                g.FillRectangle(bg, ClientRectangle);


            using (Brush fg = new SolidBrush(ForeColor))
                foreach (Circle c in circles)
                    c.OnPaint(g, fg);
        }

        #region IEditorView Members

        public Control Control
        {
            get { return this; }
        }

        public void Subscribe(IDocumentReader document)
        {
            throw new NotImplementedException();
        }

        public void Unsubscribe(IDocumentReader document)
        {
            throw new NotImplementedException();
        }

        #endregion

        private void EditorView_MouseMove(object sender, MouseEventArgs e)
        {

        }

        private void EditorView_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                circles.Add(new Circle(e.X, e.Y, defaultRadius));

            else if (e.Button == MouseButtons.Right)
            {
                foreach (Circle c in circles)
                {
                    if (Distance(e.X, e.Y, c.X, c.Y) < c.Radius)
                    {
                        circles.Remove(c);
                        break;
                    }
                }
            }

            Invalidate();            
        }

        private void EditorView_MouseUp(object sender, MouseEventArgs e)
        {

        }

        private void EditorView_MouseLeave(object sender, EventArgs e)
        {

        }

        private void EditorView_KeyUp(object sender, KeyEventArgs e)
        {

        }

        private static float Distance(float x1, float y1, float x2, float y2)
        {
            float x = x1 - x2;
            float y = y1 - y2;
            return (float)Math.Sqrt((x * x) + (y * y));
        }
    }

    class Circle
    {
        public float X { get; set; }
        public float Y { get; set; }
        public float Radius { get; set; }

        public Circle(float x, float y, float r)
        {
            X = x;
            Y = y;
            Radius = r;
        }

        public void OnPaint(Graphics g, Brush b)
        {
            g.FillEllipse(b, X, Y, Radius, Radius);
        }
    }
}
